/**
 * 
 */
package com.mc.okhttp;

import java.io.IOException;

import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

/**
 * okhttp 摘要加密认证
 * 
 * @author w.weizhen
 *
 */
public class AuthenticatorTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		
		
		
	}
	
	
	
	private static void test0()
	{
		try {
			OkHttpClient client = new OkHttpClient.Builder()
	        .authenticator(new Authenticator() {
	            @Override
	            public Request authenticate(Route route, Response response) {
	                System.out.println("Authenticating for response: " + response);
	                System.out.println("Challenges: " + response.challenges());
	                String credential = Credentials.basic("jesse", "password1");
	                return response.request().newBuilder()
	                        .header("Authorization", credential)
	                        .build();
	            }
	        })
	        .build();
			
			Request request = new Request.Builder()
		            .url("http://publicobject.com/secrets/hellosecret.txt")
		            .build();

		    Response response = client.newCall(request).execute();
		    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
		    System.out.println(response.body().string());
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
	    
	    
	    
	}
	
	
	
	
	
}